<script type="text/javascript" charset="utf-8">
    function reload_permissions(user_id)
    {
        new Ajax.Updater('user_permissions','<?php echo url_for(array('controller'=>'admin/users','action'=>'get_user_permissions')) ?>
        ',{parameters:'user_id='+user_id});
    }
</script>
<h1>User Management</h1>

<!-- Select User To Manage -->
<div id="select_new">
<?php echo form_tag('index',array('method'=>'get'))?>
<?php echo link_to('Add New User',array('controller'=>'admin/users' )
)?> |
Get Details For: <?php echo select_tag('id',options_for_select($all_users, (int) @$_REQUEST['id']))?>
<?php echo submit_tag('Edit')?>
<?php echo end_form_tag()?>
</div>

<?php
    if ( !$users->is_loaded() )
    {
?>
<div class='yui-b'>
    
        <h3 class='tab'>Add User</h3>      
        <?php echo form_tag('index',array('id'=>'add-form'))?>
        <?php echo hidden_field_tag('action','add_user')?>
        <?php include load_template('_form.php')?>
        <?php echo end_form_tag()?>
        <?php echo validate_form('add-form',array('useTitles'=>'true')) ?>
    
</div>
<?php } else { ?>
<?php echo error_messages_for('users')?>
<?php echo content_tag('h2',"Managing User: <span style='color:red'>".$users->fullname()."</span>") ?>
<div id='tabsmenu'>
    <ul id='tabs'>
        <li><a href="#tab1">Edit user details</a></li>
        <li><a href="#tab2">Roles this user has</a></li>
        <li><a href="#tab3">Permissions for this user</a></li>
    </ul>
</div>   
<div id="tab1" class='tab-page'>
    <h3>Details</h3>
    <div class='yui-g'>
        <div class='yui-u first'>
            <?php echo form_tag('index',array('id'=>'edit-form'))?>
            <?php echo hidden_field_tag('action','edit_user')?>
            <?php include load_template('_form_edit.php')?>
            <?php echo end_form_tag()?>
            <?php echo validate_form('edit-form',array('useTitles'=>'true')) ?>
        </div>
    
        <div class='div-delete yui-u'>
            <?php echo  form_tag('delete') ?>
            <?php echo  hidden_field_tag('id',$users->get('id')) ?>
            <?php echo  hidden_field_tag('action','delete') ?>
            <span style='color:red;font-weight:bold'>Delete This User?</span>
            <?php echo  submit_tag('Delete User',array('confirm'=>'Are you sure you want to delete this user?',
                                                       'class'=>'submit_delete')) ?>
            <?php echo  end_form_tag() ?>
        </div>
    </div>    
</div>
    
<div id="tab2" class='tab-page'>
    <h3>Roles</h3>
    <div class='user_roles' style='width:350px;'>
    <?php echo form_remote_tag(array('controller'=>'admin/roles' ,'action'=>'modify_user_roles'),
                               array(
                                    'name'=>'user_roles',
                                    'update'=>'success_messages',
                                    'before'=>"selectAll('roles')",
                                    'complete'=>"toggle_success();reload_permissions(".$users->get('id').");",
                                 )
                            );
     ?>
    <?php echo hidden_field_tag('user_id', $users->get('id'))?>
    <?php echo select_transfer('roles[]', array_diff($all_roles,$user_roles), $user_roles)?>  
    <div id='submit'><?php echo submit_tag('Modify Roles')?></div>
    <?php echo end_form_tag()?>
   </div>
</div>
    
<div id="tab3" class='tab-page'>
    <h3>Permissions</h3>
    <div id='user_permissions'>
        <p>Permissions can be set per user, and they are also inherited from roles.</p>
    <?php include load_template('_permissions.php');?>
    
    <br style='clear:both'>
    </div>
</div>
    
<?php } ?>